<?php
/**
 * Created by PhpStorm.
 * User: jiangfb
 * Date: 2016/9/22
 * Time: 14:09
 */

namespace mybackend\controllers;

use common\entities\myhtsq\ActionCode;
use common\error\ErrorCode;
use Yii;
use mybackend\controllers\BaseController;
use common\services\userauth\UserAuthService;
use yii\helpers\ArrayHelper;

class UserAuthController extends BaseController
{
    protected $_userAuthService;

    public function __construct($id, $module, UserAuthService $userAuthService, $config = [])
    {
        $this->_userAuthService = $userAuthService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules() {
        return [
            'actions' => [ 'SaveRole','DeleteRole','RoleList','RoleInfo','GetAllAuthList','GetRoleAuthList', 'SaveRoleAuth', 'GetOrgList','WaitUserList','SaveRoleUser','UserList','GetUserInfo','SetAreaCharge','SetUserRoleAndBusiness'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'SaveRole' => ActionCode::USER_ROLE_MOD,
                        'DeleteRole' => ActionCode::USER_ROLE_MOD,
                        'RoleList' => ActionCode::USER_ROLE_VIEW,
                        'RoleInfo' => ActionCode::USER_ROLE_VIEW,
                        'GetAllAuthList' => ActionCode::USER_ROLE_VIEW,
                        'GetRoleAuthList' => ActionCode::USER_ROLE_VIEW,
                        'SaveRoleAuth'  => ActionCode::USER_ROLE_MOD,
                        'GetOrgList' => ActionCode::USER_ROLE_VIEW,
                        'WaitUserList' => ActionCode::USER_ROLE_VIEW,
                        'SaveRoleUser'=> ActionCode::USER_ROLE_MOD,
                        'UserList' => ActionCode::USER_ORG_VIEW,
                        'GetUserInfo' => ActionCode::USER_ORG_VIEW,
                        'SetAreaCharge' => ActionCode::USER_ORG_MOD,
                        'SetUserRoleAndBusiness'  => ActionCode::USER_ORG_MOD
                    ]
                ]
            ]
        ];
    }


    public function actionSaveRole(){
        $this->exitIfNotPost();
        $roleData = Yii::$app->request->post();
//        $roleData = ["role_name"=>"测试333",'comment'=>"test111", "parent_id"=>0];
        if (empty($roleData['role_name']) || !isset($roleData['parent_id'])){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $this->_userAuthService->saveRole($roleData);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>[]
        ]);
    }

    public function actionDeleteRole()
    {
        $role_id = Yii::$app->request->get('role_id');
        if (empty($role_id)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $this->_userAuthService->deleteRole($role_id);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>[]
        ]);
    }

    public function actionRoleList()
    {
        $keyword = Yii::$app->request->get('keyword');
        $roleList = $this->_userAuthService->getRoleList($keyword);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>$roleList
        ]);
    }

    public function actionRoleInfo()
    {
        $role_id = Yii::$app->request->get('role_id');
        if (empty($role_id)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $roleInfo = $this->_userAuthService->getRoleInfo($role_id);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['roleInfo' => $roleInfo]
        ]);
    }

    /**
     * 权限点列表
     */
    public function actionGetAllAuthList()
    {
        $authList = $this->_userAuthService->getAllAuthList();
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['authList' => $authList]
        ]);
    }

    /**
     * 获取单个角色权限点
     */
    public function actionGetRoleAuthList()
    {
        $role_id = Yii::$app->request->get('role_id');
        $authList = $this->_userAuthService->getRoleAuthList($role_id);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['authList' => $authList]
        ]);
    }

    /**
     * 保存角色权限
     */
    public function actionSaveRoleAuth()
    {
        $role_id = Yii::$app->request->post('role_id');
        $pockage_data = Yii::$app->request->post('actions');
//        $pockage_data = [1,2];
        if (empty($role_id)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $result = $this->_userAuthService->saveRoleAuth($role_id, $pockage_data);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['result' => $result]
        ]);
    }

    /**
     * 组织列表
     */
    public function actionGetOrgList()
    {
        $orgList = $this->_userAuthService->getOrgList();
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['orgList' => $orgList]
        ]);
    }

    /**
     * 待选用户列表
     */
    public function actionWaitUserList()
    {
        $role_id = Yii::$app->request->get('role_id');
        if (empty($role_id)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $buGUID = Yii::$app->request->get('buGUID');
        $keyword = Yii::$app->request->get('keyword');
        $userList = $this->_userAuthService->getWaitUser($buGUID, $keyword, $role_id);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['userList' => $userList]
        ]);
    }

    public function actionGetLdapUserList()
    {
        $buGUID = Yii::$app->request->get('buGUID');
        $keyword = Yii::$app->request->get('keyword');
        $userList = $this->_userAuthService->getUserListByBuGUIDAndKeyword($buGUID, $keyword);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['userList' => $userList]
        ]);
    }

    /**
     * 用户批量绑定角色
     */
    public function actionSaveRoleUser()
    {
        $role_id = Yii::$app->request->post('role_id');
        if (empty($role_id)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $userList = json_decode(Yii::$app->request->post('userList'),true);
        
        $result = $this->_userAuthService->saveRoleUser($role_id, $userList);
        foreach($userList as $k => $v){
            Yii::$app->cache->delete('role_auth_actions_cache_key_'.$v);
        }

        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['result' => $result]
        ]);

    }

    /**
     * 组织用户列表
     */
    public function actionUserList()
    {
        $buGUID = Yii::$app->request->get('buGUID');
        $keyword = Yii::$app->request->get('keyword');
        $pageIndex = Yii::$app->request->get('pageIndex', 1);
        $perPage = Yii::$app->request->get('pageSize', 10);
        $offset = ($pageIndex-1) * $perPage;
        $userData = $this->_userAuthService->getUserList($buGUID, $keyword, $perPage, $offset);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['userList' => $userData['userList'], 'pageTotal' => $userData['pageTotal']]
        ]);
    }

    /**
     * 获取用户信息
     */
    public function actionGetUserInfo()
    {
        $userGUID = Yii::$app->request->get('userGUID');
        if (empty($userGUID)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $userInfo = $this->_userAuthService->getUserInfo($userGUID);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['userInfo' => $userInfo]
        ]);
    }

    /**
     * 设置取消区域管理员
     */
    public function actionSetAreaCharge()
    {
        $userGUID = Yii::$app->request->get('userGUID');
        if (empty($userGUID))
        {
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $this->_userAuthService->setAreaCharge($userGUID);
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>[]
        ]);
    }

    /**
     * 设置用户角色和兼职公司
     */
    public function actionSetUserRoleAndBusiness()
    {
        $userGUID = Yii::$app->request->post('userGUID');
        if (empty($userGUID)){
            return $this->json([
                "retCode"=> ErrorCode::USER_AUTH__INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>[]
            ]);
        }
        $roleList = json_decode(Yii::$app->request->post('roleList'), true);
        $partTimeBUGUIDList = json_decode(Yii::$app->request->post('partTimeBUGUIDList'), true);
        $this->_userAuthService->setUserRoleAndBusiness($userGUID, $roleList, $partTimeBUGUIDList);

        Yii::$app->cache->delete('role_auth_actions_cache_key_'.$userGUID);

        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>[]
        ]);
    }

    /**
     * 获取当前用户所有权限
     */
    public function actionGetUserAllAuth()
    {
        $userAuth = $this->_userAuthService->getCurrUserAuthList();
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['userAuth' => $userAuth]
        ]);
    }

    /**
     * 判断角色是否关联用户
     */
    public function actionCheckHasBind()
    {
        $role_id = Yii::$app->request->get('role_id');
        $result = $this->_userAuthService->checkRole($role_id);
        $result = ($result > 0) ? 1 : 0;
        return $this->json([
            "retCode"=> $this->_userAuthService->getErrCode(),
            "msg" => $this->_userAuthService->getErrMsg(),
            "data"=>['result' => $result]
        ]);
    }



}
